package com.parkreport.admin.parkarea.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.common.base.BaseService;
import com.common.jdbc.DB4ObjUtil;
import com.common.jdbc.DBBaseUtil;
import com.common.util.StringUtil;
import com.parkreport.vo.DmbVO;
import com.parkreport.vo.ParkDmbVO;
import com.sysadmin.vo.JtreeVO;

public class DmbService extends BaseService {

	public List<ParkDmbVO> find(String areaId){
		String sql = "select p.ID as id,p.NID as nid,p.PARK_AREA_ID as PARK_AREA_ID,p.TYPE as type,d.ROAD as road from PARK_DMB p left join QUERY_DMB d on p.NID=d.NID  where 1=1 and p.PARK_AREA_ID=?";
		List<ParkDmbVO> list = DB4ObjUtil.execute4Obj(new ParkDmbVO(), sql,areaId);
		
		return list;
	}
	
	public String saveList(Connection conn,List<ParkDmbVO> list,String parkAreaId){
		
		String delsql = "delete from PARK_DMB where PARK_AREA_ID=?";
		if(!StringUtil.isBlank(parkAreaId)){
			DBBaseUtil.executeUpdateByConn(conn,delsql, parkAreaId);
			
		}
		if(list!=null&&list.size()>0){
				for (int i = 0; i < list.size(); i++) {
					ParkDmbVO vo = list.get(i);
					if(vo!=null){
						String id = StringUtil.getUuid();
						String sql = "insert into PARK_DMB(ID,PARK_AREA_ID,NID,TYPE,DEL_FLAG) values(?,?,?,?,?)";
						DBBaseUtil.executeUpdateByConn(conn,sql,id, parkAreaId, vo.getNid(),vo.getType(),"0");
					}
				}
		}
		return "1";
	}
	
	public List<DmbVO> findDmb(){
		String sql = "select * from QUERY_DMB  where 1=1 and DEL_FLAG='0'";
		List<DmbVO> list = DB4ObjUtil.execute4Obj(new DmbVO(), sql);
		
		return list;
	}

	public String getDmbTree(){
		List<DmbVO> list = findDmb();
		
		Document doc = DocumentHelper.createDocument();
		Element tree = doc.addElement("item");
		tree.addAttribute("id","0");
		for (int i = 0; i < list.size(); i++) {
			Element e = tree.addElement("item");
			 e.addAttribute("id", String.valueOf(list.get(i).getNid()));
			 e.addAttribute("text", list.get(i).getRoad());
			
			
		}
		return doc.asXML();
	}
	
	
	public String getDmbJson(){
		List<DmbVO> list = findDmb();
		List<JtreeVO> treeList = new ArrayList<JtreeVO>();
		for (int i = 0; i < list.size(); i++) {
			DmbVO v = list.get(i);
			JtreeVO jtree = new JtreeVO();
			jtree.setId(v.getNid());
			jtree.setName(v.getRoad());
			
			treeList.add(jtree);
		}
		
		JsonConfig jsonConfig = new JsonConfig();  
		jsonConfig.setJsonPropertyFilter( new PropertyFilter(){  
		public boolean apply(Object source, String name, Object value ){  
			if(StringUtil.isBlank(value)){
				return true;
			}
			if (value instanceof java.util.ArrayList) {
				ArrayList l = (ArrayList) value;
				if(l.size()==0){
					return true;
				}
			}
		    return false;  
		}  
		});  

		 JSONArray jsonObject =  JSONArray.fromObject(treeList,jsonConfig);
		return jsonObject.toString();
	}
	
	
	/*public String isApply(String carNum,String id){
		String sql = "select * from CAR_AREA where 1=1 and carNum=? and CarAreaID<>?";
		List<ParkDmbVO> list = DB4ObjUtil.execute4Obj(new ParkDmbVO(), sql, carNum,id);
		if(list.size()>0){
			return "1";
		}
		return "0";
	}
	
	public String isApply(ParkDmbVO vo){
		String sql = "select * from CAR_AREA where 1=1 and carNum=? and CarAreaID<>?";
		List<ParkDmbVO> list = DB4ObjUtil.execute4Obj(new ParkDmbVO(), sql, vo.getCarnum(),vo.getCarareaid());
		if(list.size()>0){
			return "1";
		}
		return "0";
	}*/
	
}
